/**
 * 
 */
package de.cbf.cam.dsa.tavern.carte.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import de.cbf.cam.dsa.tavern.carte.exceptions.WrongTypeProvidedException;
import de.cbf.cam.dsa.tavern.carte.model.entities.Ingredient;
import de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.IEditableEntity;
import de.cbf.cam.dsa.tavern.carte.ui.CompositeEditIngredients;
import de.cbf.cam.dsa.tavern.carte.ui.interfaces.ICompositeEditEntities;

/**
 * @author Stefan Alter
 *
 */
public class ControllerEditIngredients extends ControllerEditEntities {

	/**
	 * 
	 */
	public ControllerEditIngredients() {
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#checkEntity(de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.IEditableEntity)
	 */
	@Override
	protected boolean checkEntity(IEditableEntity value)
			throws IllegalArgumentException {
		if (value instanceof Ingredient) {
			return true;			
		}
		if(value == null){
			throw new WrongTypeProvidedException(Ingredient.class.getName(),"null");	
		}
		throw new WrongTypeProvidedException(Ingredient.class, value.getClass());		
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#checkView(de.cbf.cam.dsa.tavern.carte.ui.interfaces.ICompositeEditEntities)
	 */
	@Override
	protected boolean checkView(ICompositeEditEntities value)
			throws IllegalArgumentException {
		if (value instanceof CompositeEditIngredients) {			
			return true;
		}
		if(value == null){
			throw new WrongTypeProvidedException(CompositeEditIngredients.class.getName(),"null");	
		}
		throw new WrongTypeProvidedException(CompositeEditIngredients.class, value.getClass());	
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#getEntitiesChangedListener()
	 */
	@Override
	protected PropertyChangeListener getEntitiesChangedListener() {
		return pclIngredients;
	}
	private PCLIngredients pclIngredients = new PCLIngredients(); 
	private class PCLIngredients implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent arg0) {
			view.setEntities(entities.iterator());			
		}	
	}
	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#getNewEntity()
	 */
	@Override
	protected IEditableEntity getNewEntity() {
		return new Ingredient();
	}
	public Collection<Ingredient> getEditedIngredients(){
		List<Ingredient> resultList = new ArrayList<Ingredient>();
		Iterator<IEditableEntity> iterator = entities.iterator();
		while (iterator.hasNext()) {
			IEditableEntity iEditableEntity = (IEditableEntity) iterator.next();
			if (iEditableEntity instanceof Ingredient) {
				Ingredient processing = (Ingredient) iEditableEntity;
				resultList.add(processing);
			}
		}		
		return resultList;
	}
	
	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#registerSubControllerListeners(de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntity)
	 */
	@Override
	protected void registerSubControllerListeners(ControllerEditEntity value) {
		super.registerSubControllerListeners(value);
		if (value instanceof ControllerEditIngredient) {
			ControllerEditIngredient controller = (ControllerEditIngredient) value;
			controller.registerChangeParentListener(changeParent);
			controller.registerAddCategoryListener(addCategory);
			controller.registerAddAddendumListener(addAddendum);
		}	
		
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#deRegisterSubControllerListeners(de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntity)
	 */
	@Override
	protected void deRegisterSubControllerListeners(ControllerEditEntity value) {
		super.deRegisterSubControllerListeners(value);
		if (value instanceof ControllerEditIngredient) {
			ControllerEditIngredient controller = (ControllerEditIngredient) value;
			controller.deRegisterChangeParentListener(changeParent);
			controller.deRegisterAddCategoryListener(addCategory);
			controller.deRegisterAddAddendumListener(addAddendum);
		}
	}

	
//	============================
//		overwritten behavior
//	============================

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#doSelectEntity(de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.IEditableEntity)
	 */
	@Override
	public void doSelectEntity(IEditableEntity value) {
		if (controllerEditEntity instanceof ControllerEditIngredient) {
			ControllerEditIngredient controller = (ControllerEditIngredient) controllerEditEntity;
			if(value instanceof Ingredient){
				Ingredient ingredient = (Ingredient) value;
				controller.ingredientSelected(ingredient);
			}
		}
	}

	
	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#doDoubleClickEntity(de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.IEditableEntity)
	 */
	@Override
	public void doDoubleClickEntity(IEditableEntity value) {
		//We do a normal selection on doubleClick
		super.doSelectEntity(value);		
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.controller.ControllerEditEntities#doNewEntity()
	 */
	@Override
	public IEditableEntity doNewEntity() {
		IEditableEntity entity = super.doNewEntity();
		doDoubleClickEntity(entity);
		return entity;		
	}	
	
	public void doChangeParent(){
		if (view instanceof CompositeEditIngredients 
				&& controllerEditEntity instanceof ControllerEditIngredient) {
			CompositeEditIngredients composite = (CompositeEditIngredients) view;			
			ControllerEditIngredient controller = (ControllerEditIngredient) controllerEditEntity;
			//
			IEditableEntity entity = composite.getEntitySelected();
			if (entity instanceof Ingredient) {
				Ingredient ingredient = (Ingredient) entity;
				controller.doChangeParentTo(ingredient);
			}				
		}
	}
	public void doAddCategory(){
		if (view instanceof CompositeEditIngredients 
				&& controllerEditEntity instanceof ControllerEditIngredient) {
			CompositeEditIngredients composite = (CompositeEditIngredients) view;			
			ControllerEditIngredient controller = (ControllerEditIngredient) controllerEditEntity;
			//
			IEditableEntity entity = composite.getEntitySelected();
			if (entity instanceof Ingredient) {
				Ingredient ingredient = (Ingredient) entity;
				controller.doAddCategory(ingredient);
			}				
		}
	}
	public void doAddAddendum(){
		if (view instanceof CompositeEditIngredients 
				&& controllerEditEntity instanceof ControllerEditIngredient) {
			CompositeEditIngredients composite = (CompositeEditIngredients) view;			
			ControllerEditIngredient controller = (ControllerEditIngredient) controllerEditEntity;
			//
			IEditableEntity entity = composite.getEntitySelected();
			if (entity instanceof Ingredient) {
				Ingredient ingredient = (Ingredient) entity;
				controller.doAddAddendum(ingredient);
			}				
		}
	}
	
	private ChangeParentAdvancer changeParent = new ChangeParentAdvancer();	
	private class ChangeParentAdvancer implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			doChangeParent();
		}		
	}
	private AddCategoryAdvancer addCategory = new AddCategoryAdvancer();	
	private class AddCategoryAdvancer implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			doAddCategory();
		}		
	}
	private AddAddendumAdvancer addAddendum = new AddAddendumAdvancer();	
	private class AddAddendumAdvancer implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			doAddAddendum();
		}		
	}
}
